package cn.wolfcode.crm.service.impl;
import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.mapper.DepartmentMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class DepartmentServiceImpl implements IDepartmentService {
    //private static final Logger log = LoggerFactory.getLogger(DepartmentServiceImpl.class);
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
        //记录日志
        log.debug("{}用户删除了id为:{}的部门信息", UserContext.getCurrentUser().getName(), id);

    }




@Override
@Transactional
public void save(Department department) {

departmentMapper.insert(department);
}

@Override
public Department get(Long id) {
return departmentMapper.selectByPrimaryKey(id);
}

@Override
public List<Department> listAll() {
return departmentMapper.selectAll();
}

@Override
public void update(Department department) {
departmentMapper.updateByPrimaryKey(department);
}

public PageInfo query(QueryObject qo) {
//使用分页插件,传入当前页,每页显示数量
PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
//查询结果集
List<Department> list = departmentMapper.selectForList(qo);
return new PageInfo(list);
}
}
